<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>公司列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-form :inline="true" :model="queryinfo" class="demo-form-inline" size="mini">
        <el-form-item >
          <el-input v-model="queryinfo.title" placeholder="用户昵称" clearable/>
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryinfo.comid" placeholder="所属公司" clearable filterable>
            <el-option
                v-for="item in company"
                :key="item.comid"
                :label="item.title"
                :value="item.comid"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item >
          <el-input v-model="queryinfo.account" placeholder="用户账号" clearable/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryinfo.state" clearable filterable>
            <el-option label="正常" value="1"></el-option>
            <el-option label="拉黑" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间选择">
          <el-date-picker
              v-model="timer"
              type="datetimerange"
              range-separator="-"
              start-placeholder="Start date"
              end-placeholder="End date"
              :default-time="defaultTime"
              @change="changeData"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="GetData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="companyOpen(1)">创建</el-button>
        </el-form-item>
      </el-form>
      <hr>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="username" label="用户名" width="180"/>
        <el-table-column prop="title" label="所属公司" width="180"/>
        <el-table-column prop="account" label="账号" width="180"/>
        <el-table-column prop="bean" label="金豆" width="180"/>
        <el-table-column prop="state" label="状态" width="180">
          <template #default="props">
            <el-tag size="mini" v-if="props.row.state === 1" type="success">正常</el-tag>
            <el-tag size="mini" v-else type="warning">拉黑</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="addtime" label="注册时间">
          <template #default="props">{{ formatDate(props.row.addtime) }}</template>
        </el-table-column>
        <el-table-column prop="uptime" label="更新时间">
          <template #default="props">{{ formatDate(props.row.uptime) }}</template>
        </el-table-column>

        <el-table-column label="##">
          <template #default="props">
            <el-button type="primary" @click="companyOpen(props.row)" size="mini">Edit</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <el-pagination
          background
          :current-page="queryinfo.page"
          :page-sizes="[3,10, 50, 100, 1000]"
          :page-size="queryinfo.size"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="changePage"
      />

      <!--      添加修改-->
      <el-drawer
          v-model="userDrawer"
          :title="userTitle === 1?'添加':'更新'"
          direction="rtl"
          :before-close="handleClose"
      >
        <el-form :model="userform" label-width="120px">
          <el-form-item label="用户名称">
            <el-input v-model="userform.username" :disabled="userTitle===1?false:true"></el-input>
          </el-form-item>
          <el-form-item label="用户账号">
            <el-input v-model="userform.account" :disabled="userTitle===1?false:true"></el-input>
          </el-form-item>
          <el-form-item label="所属公司">
          <el-select v-model="userform.comid" placeholder="所属公司" clearable filterable :disabled="userTitle===1?false:true">
            <el-option
                v-for="item in company"
                :key="item.comid"
                :label="item.title"
                :value="item.comid"
            >
            </el-option>
          </el-select>
          </el-form-item>

          <el-form-item label="状态">
            <el-switch v-model="userform.state" active-color="#13ce66" inactive-color="#ff4949" :active-value='1'
                       :inactive-value='2'/>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="userform.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="sumbit">{{ userTitle === 1 ? "Create" : "Updata" }}</el-button>
          </el-form-item>
        </el-form>
      </el-drawer>

    </el-card>
  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from 'vue'
import {formatDate} from '@/utils/date'
import {ElMessage} from "element-plus";
import axios from "@/utils/axios";

export default {
  name: 'Good',
  setup() {
    const state = reactive({
      queryinfo: {
        page: 1,
        size: 10
      },
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      total: 0,
      tableData: [],
      company:[],
      timer: [],
      defaultTime: [
        new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),
      ],


      userform: {},
      userDrawer: false,
      userTitle: 1,
    })
    onMounted(() => {
      GetData()
      GetCompany()
    })
    //时间操作
    const changeData = async (row) => {
      if (row !== null) {
        state.queryinfo.startTime = ((new Date(row[0])).getTime()) / 1000
        state.queryinfo.endTime = ((new Date(row[1])).getTime()) / 1000
      } else {
        delete state.queryinfo.startTime
        delete state.queryinfo.endTime
      }
    }

    //打开关闭抽屉
    const companyOpen = (data) => {
      state.userDrawer = true
      if (data === 1) {
        state.userform = {state: 2}
        state.userTitle = 1
      } else {
        state.userform = data
        state.userTitle = 2
      }
    }
    const handleClose = () => {
      state.userDrawer = false
    }

    //提交更新/新加
    const sumbit = async () => {
      if (state.userTitle === 1) {
        const {data: res} = await axios.put('user', state.userform)
        if (res.code !== 1000) return ElMessage.error(res.msg)
        GetData()
      } else {
        const {data: res} = await axios.post('user', state.userform)
        if (res.code !== 1000) return ElMessage.error(res.msg)
      }
      ElMessage.success("成功")
      state.userDrawer = false
    }

    const GetCompany = async () => {
      const {data: res} = await axios.get('user/company')
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.company = res.data
    }

    // 获取列表
    const GetData = async () => {
      const {data: res} = await axios.get('user', {params: state.queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.tableData = res.data.list
      state.total = res.data.total
    }
    const changePage = (val) => {
      console.log(11)
      state.queryinfo.page = val
      GetData()
    }
    const handleSizeChange = (val) => {
      state.queryinfo.size = val
      GetData()
    }
    return {
      ...toRefs(state),
      formatDate,
      GetData,
      changePage,
      handleSizeChange,
      changeData,
      companyOpen,
      handleClose,
      sumbit,GetCompany

    }
  }
}

</script>

<style scoped>

</style>